// 定义数据格式

/***
 * 
 * "wxSearchData":{
 *  configconfig:{
 *    style: "wxSearchNormal"
 *  },
 *  view:{
 *    hidden: true,
 *    searchbarHeght: 20
 *  }
 *  keys:[],//自定义热门搜索
 *  his:[]//历史搜索关键字
 *  value
 * }
 * 
 * 
 */
var __keysColor = [];

var __mindKeys = [];

function initColors(colors) {
  __keysColor = colors;
}

function initMindKeys(keys) {
  __mindKeys = keys;
}

function init(that, barHeight, keys, isShowKey, isShowHis, callBack) {
  var temData = {};
  var view = {
    barHeight: barHeight,
    isShow: false
  }

  if (typeof(isShowKey) == 'undefined') {
    view.isShowSearchKey = true;
  } else {
    view.isShowSearchKey = isShowKey;
  }

  if (typeof(isShowHis) == 'undefined') {
    view.isShowSearchHistory = true;
  } else {
    view.isShowSearchHistory = isShowHis;
  }
  temData.keys = keys;
  wx.getSystemInfo({
    success: function(res) {
      var wHeight = res.windowHeight;
      view.seachHeight = wHeight - barHeight;
      temData.view = view;
      temData.selected = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
      that.setData({
        wxSearchData: temData
      });
    }
  })

  if (typeof(callBack) == "function") {
    callBack();
  }

  getHisKeys(that);
}

function wxSearchInput(e, that, callBack) {
  var temData = that.data.wxSearchData;
  var text = e.detail.value;
  var mindKeys = [];
  if (typeof(text) == "undefined" || text.length == 0) {

  } else {
    for (var i = 0; i < __mindKeys.length; i++) {
      var mindKey = __mindKeys[i];
      if (mindKey.indexOf(text) > -1) {
        mindKeys.push(mindKey);
      }
    }
  }
  temData.value = text;
  temData.mindKeys = mindKeys;
  that.setData({
    wxSearchData: temData
  });
}

function wxSearchFocus(e, that, callBack) {
  var temData = that.data.wxSearchData;
  temData.view.isShow = true;
  that.setData({
    wxSearchData: temData
  });
  //回调
  if (typeof(callBack) == "function") {
    callBack();
  }
  // if(typeof(temData) != "undefined"){
  //   temData.view.hidden= false;
  //   that.setData({
  //     wxSearchData:temData
  //   });
  // }else{

  // }
}

function wxSearchBlur(e, that, callBack) {
  var temData = that.data.wxSearchData;
  temData.value = e.detail.value;
  that.setData({
    wxSearchData: temData
  });
  if (typeof(callBack) == "function") {
    callBack();
  }
}

function wxSearchHiddenPancel(that) {
  var temData = that.data.wxSearchData;
  temData.view.isShow = false;
  that.setData({
    wxSearchData: temData
  });
}

function wxSearchLabelTap(e, that, callBack) {
  //回调
  var temData = that.data.wxSearchData;
  var selected = temData.selected;
  var index = e.target.dataset.index;
  if (selected[index]) {
    selected[index] = 0
  } else {
    selected[index] = 1
  }
  temData.selected = selected;

  that.setData({
    wxSearchData: temData
  });
  // if (typeof (callBack) == "function") {
  //   callBack();
  // }
}

function wxSearchKeyTap(e, that, callBack) {
  //回调
  var temData = that.data.wxSearchData;
  temData.value = e.target.dataset.key;
  that.setData({
    wxSearchData: temData
  });
  if (typeof(callBack) == "function") {
    callBack();
  }
}

function getHisKeys(that) {
  var value = [];
  try {
    value = wx.getStorageSync('wxSearchHisKeys')
    if (value) {
      // Do something with return value
      var temData = that.data.wxSearchData;
      temData.his = value;
      that.setData({
        wxSearchData: temData
      });
    }
  } catch (e) {
    // Do something when catch error
  }

}

function wxSearchAddHisKey(that) {
  wxSearchHiddenPancel(that);
  var text = that.data.wxSearchData.value;
  if (typeof(text) == "undefined" || text.length == 0) {
    return;
  }
  var value = wx.getStorageSync('wxSearchHisKeys');
  if (value) {
    if (value.indexOf(text) < 0) {
      value.unshift(text);
    }
    wx.setStorage({
      key: "wxSearchHisKeys",
      data: value,
      success: function() {
        getHisKeys(that);
      }
    })
  } else {
    value = [];
    value.push(text);
    wx.setStorage({
      key: "wxSearchHisKeys",
      data: value,
      success: function() {
        getHisKeys(that);
      }
    })
  }


}

function wxSearchDeleteKey(e, that) {
  var text = e.target.dataset.key;
  var value = wx.getStorageSync('wxSearchHisKeys');
  value.splice(value.indexOf(text), 1);
  wx.setStorage({
    key: "wxSearchHisKeys",
    data: value,
    success: function() {
      getHisKeys(that);
    }
  })
}

function wxSearchDeleteAll(that) {
  wx.showModal({
    title: '清空搜索历史',
    content: '您确定清空所有搜索历史吗？',
    showCancel: true, //是否显示取消按钮
    cancelText: "取消", //默认是“取消”
    cancelColor: '#0000FF',
    confirmText: "确定",
    confirmColor: '#DC143C', //确定文字的颜色
    success: function(ress) {
      if (ress.cancel) {
        //点击取消,默认隐藏弹框
      } else {
        //点击确定
        wx.removeStorage({
          key: 'wxSearchHisKeys',
          success: function(res) {
            var value = [];
            var temData = that.data.wxSearchData;
            temData.his = value;
            that.setData({
              wxSearchData: temData
            });
          }
        })
        //点击确定
      }
    },
    fail: function(ress) {}, //接口调用失败的回调函数
    complete: function(ress) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
  })
}



module.exports = {
  init: init,
  initColor: initColors,
  initMindKeys: initMindKeys,
  wxSearchInput: wxSearchInput,
  wxSearchFocus: wxSearchFocus,
  wxSearchBlur: wxSearchBlur,
  wxSearchLabelTap: wxSearchLabelTap,
  wxSearchKeyTap: wxSearchKeyTap,
  wxSearchAddHisKey: wxSearchAddHisKey,
  wxSearchDeleteKey: wxSearchDeleteKey,
  wxSearchDeleteAll: wxSearchDeleteAll,
  wxSearchHiddenPancel: wxSearchHiddenPancel
}